package com.task.web.controller.core;

import javax.annotation.Resource;

import com.task.core.vo.resp.CountryDetailRespVo;
import com.task.core.vo.resp.DatasourceDetailRespVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.annotation.DataScope;
import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.dto.request.CountryRequestDTO;
import com.task.core.service.CheckService;
import com.task.core.service.CountryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.Scope;

/**
 * @author 迪迦.
 * @date 2024/8/14 15:09
 */
@Api("国家接口")
@RestController
@RequestMapping("/country")
public class CountryController extends BaseController {

  @Resource
  private CountryService countryService;

  @Resource
  private CheckService checkService;


  @PreAuthorize("@ss.hasPermi('country:list')")
  @GetMapping("/getCountryByPage")
  public TableDataInfo getCountryByPage(CountryRequestDTO request) {
    startPage(request.getPageNum(), request.getPageSize());
    return getDataTable(countryService.getCountryByPage(request));
  }


  @PreAuthorize("@ss.hasPermi('country:option')")
  @GetMapping("/getCountryForOption")
  public TableDataInfo getCountryForOption(CountryRequestDTO request) {
    return getDataTable(countryService.getCountryForOption(request));
  }

  @Log(title = "国家管理", businessType = BusinessType.INSERT)
  @PostMapping("/addCountry")
  public AjaxResult addCountry(@RequestBody CountryRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    countryService.addCountry(request);
    return success();
  }

  @Log(title = "国家管理", businessType = BusinessType.UPDATE)
  @PutMapping("/modifyCountry")
  public AjaxResult modifyCountry(@RequestBody CountryRequestDTO request) {
    checkService.check("data_country", request.getCode());
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    countryService.modifyCountry(request);
    return success();
  }

  @Log(title = "国家管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteCountry/{countryCode}")
  public AjaxResult deleteCountry(@PathVariable String countryCode) {
    checkService.check("data_country", countryCode);
    countryService.deleteCountry(countryCode, SecurityUtils.getLoginUser().getUserId());
    return success();
  }

  @GetMapping ("/getCountryDetail/{countryCode}")
  @PreAuthorize("@ss.hasPermi('system:country:edit')")
  public AjaxResult getCountryDetail(@PathVariable String countryCode) {
    checkService.check("data_country", countryCode);
    CountryDetailRespVo countryDetail = countryService.getCountryDetail(countryCode);
    return success(countryDetail);
  }
}
